# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Saul Ibarra Corretge <saghul@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

pkgbase=p11-kit
pkgname=('p11-kit' 'libp11-kit' 'libp11-kit-devel')
pkgver=0.23.22
pkgrel=2
pkgdesc="Library to work with PKCS#11 modules"
arch=('i686' 'x86_64')
url="https://p11-glue.freedesktop.org/p11-kit.html"
license=('BSD')
makedepends=('bash-completion'
             'glib2-devel'
             'gtk-doc'
             'gettext-devel'
             'libiconv-devel'
             'libtasn1-devel'
             'libffi-devel'
             'meson'
             'ninja'
             'pkg-config')
options=('!debug' 'strip' 'staticlibs')
source=(https://github.com/p11-glue/p11-kit/releases/download/${pkgver}/${pkgbase}-${pkgver}.tar.xz{,.sig}
        0016-SIZE_MAX-define.patch)
validpgpkeys=('C0F67099B808FB063E2C81117BFB1108D92765AF'  # Stef Walter
              '462225C3B46F34879FC8496CD605848ED7E69871') # Daiki Ueno
sha256sums=('8a8f40153dd5a3f8e7c03e641f8db400133fb2a6a9ab2aee1b6d0cb0495ec6b6'
            'SKIP'
            '0c4ac59f72060371b956e5d6f9d2f547aee88d56f7c1b9cbf57cebf634337ee0')

prepare() {
  cd "${srcdir}"/${pkgbase}-${pkgver}
  patch -p1 -i ${srcdir}/0016-SIZE_MAX-define.patch
}

build() {
  [[ -d ${srcdir}/build ]] && rm -rf ${srcdir}/build
  mkdir -p "${srcdir}/build" && cd "${srcdir}/build"

  PKG_CONFIG_PATH="${PKG_CONFIG_PATH}:/usr/share/pkgconfig" \
  /usr/bin/meson \
    --prefix=/usr \
    --buildtype debugoptimized \
    -Dgtk_doc=true \
    -Dman=true \
    -Dtrust_paths=/etc/pki/ca-trust/source:/usr/share/pki/ca-trust-source \
    ../${pkgbase}-${pkgver}

  ninja

  DESTDIR="${srcdir}/dest" ninja install
  install -Dm644 ../${pkgbase}-${pkgver}/COPYING ${srcdir}/dest/usr/share/licenses/${pkgbase}/COPYING
}

package_p11-kit() {
  depends=("libp11-kit=${pkgver}")

  mkdir -p ${pkgdir}/usr/{bin,lib,share}
  cp -rf ${srcdir}/dest/etc ${pkgdir}/
  cp -f ${srcdir}/dest/usr/bin/*.exe ${pkgdir}/usr/bin/
  mkdir -p ${pkgdir}/usr/lib/pkcs11
  mkdir -p ${pkgdir}/usr/share/p11-kit/modules
  cp -f ${srcdir}/dest/usr/lib/pkcs11/*.dll ${pkgdir}/usr/lib/pkcs11/
  cp -f ${srcdir}/dest/usr/share/p11-kit/modules/*trust* ${pkgdir}/usr/share/p11-kit/modules/
  cp -rf ${srcdir}/dest/usr/libexec ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/share/{gtk-doc,licenses,locale,man} ${pkgdir}/usr/share/
}

package_libp11-kit() {
  depends=('libffi' 'libintl' 'libtasn1' 'glib2')
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/
}

package_libp11-kit-devel() {
  pkgdesc="Libp11-kit headers and libraries"
  groups=('development')
  depends=("libp11-kit=${pkgver}")

  mkdir -p ${pkgdir}/usr/lib/pkcs11
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib/pkgconfig ${pkgdir}/usr/lib/
  cp -f ${srcdir}/dest/usr/lib/*.a ${pkgdir}/usr/lib/
  cp -f ${srcdir}/dest/usr/lib/pkcs11/*.a ${pkgdir}/usr/lib/pkcs11/
}
